<?php
    class Admincp_PageController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_validate;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Page();
            $this->_validate = new Admincp_Form_ValidatePage();
            $this->view->headTitle('Quản lý trang');
        }
        
        public function preDispatch()
        {
            $auth = Zend_Auth::getInstance();
            if(!$auth->hasIdentity()) {
                if($this->_request->getActionName() != 'login')
                    $this->_redirect('admincp/index/login');
            }
        }
        
        public function indexAction()
        {
            $listPage = $this->_model->listItem();
            $this->view->data = $listPage;
            $this->view->total = count($listPage);
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getItem($id);
            
            if($this->_request->isPost()) {
                $description = trim($this->_arrParam['description']);

                if(!$this->_validate->validDescription($description)) {
                    $this->view->errorDescription = 'Nội dung không được để trống';
                }
                else {
                    $data = array(
                        'description'=>$description,
                    );
                    $this->_model->saveItem($data, $id);
                    $this->_redirect('admincp/page');
                }
            }
        }
    }
